/**
 * 规则：
 * 一、例如：index/index，shop/index以index结尾的，path和name默认去除index
 * 二、例如：shop/list，默认生成name为shop_list（如果结尾为index，例如shop/index则是shop）
 * 三、填写后不会自动生成
 */
let routes = [
    {
        path: '/',
        meta: {title: '登录页'},
        component: 'login/index',
        hidden: true
    },
    {
        name: 'Dashboard',
        redirect: {name: 'index'},
        component: 'layout',
        children: [
            {
                meta: {title: '后台首页',icon: 'el-icon-s-home'},
                component: 'index/index'
            }, {
                meta: {title: '商品列表',icon: "el-icon-s-shop"},
                component: 'shop/goods/list'
            },
        ]
    },
    {
        path: '/homePage',
        name: '首页配置',
        redirect: 'homePage/hotSale/index',
        component: 'layout',
        children: [
            {
                meta: {title: '热销商品',icon: 'el-icon-star-off'},
                component: 'homePage/hotSale/index'
            }, {
                meta: {title: '轮播图',icon: "el-icon-picture"},
                component: 'homePage/rotationChart/index'
            },
            {
                meta: {title: '推荐',icon: "el-icon-thumb"},
                component: 'homePage/recommend/index'
            },
            {
                meta: {title: '新品',icon: "el-icon-sell"},
                component: 'homePage/newProducts/index'
            },
        ]
    },
    {
        path: '/moduleManagement',
        name: '模块管理',
        component: 'layout',
        children: [
            {
                meta: {title: '分类管理',icon: 'el-icon-menu'},
                component: 'moduleManagement/classification/index'
            }, {
                meta: {title: '商品管理',icon: "el-icon-shopping-bag-1"},
                component: 'moduleManagement/commodity/index'
            },
            {
                meta: {title: '会员管理',icon: "el-icon-s-custom"},
                component: 'moduleManagement/member/index'
            },
            {
                meta: {title: '订单管理',icon: "el-icon-s-order"},
                component: 'moduleManagement/order/index'
            },
        ]
    },
    {
        path: '/systemManagement',
        name: '系统管理',
        component: 'layout',
        children: [
            {
                meta: {title: '修改密码',icon: 'el-icon-lock'},
                component: 'systemManagement/changePassword/index'
            },
        ]
    },
    {
        meta: {title: '登录页'},
        component: 'login/index',
        hidden: true
    },
     {
        path: '*',
        redirect: {name: 'index'}
        ,hidden: true
    }
]



// 获取路由信息方法
let getRouter = function () {
    // 生成路由详细信息
    createRouter(routes)
    return routes
}

// 自动生成路由
function createRouter(arr) {
    for (let i = 0; i < arr.length; i++) {
        if (!arr[i].component) return
        // 去除index
        let val = getValue(arr[i].component)
        // 生成name
        arr[i].name = arr[i].name || val.replace(/\//g, '_')
        // 生成path
        arr[i].path = arr[i].path || `/${val}`
        
        // 自动生成component
        let componentFun = import(`../../views/${arr[i].component}`)
        arr[i].component = () => componentFun
        if (arr[i].children && arr[i].children.length > 0) {
            createRouter(arr[i].children)
        }
    }
}

// 去除index
function getValue(str) {
    // 获取最后一个/的索引
    let index = str.lastIndexOf('/')
    // 获取最后一个/后面的值
    let val = str.substring(index + 1, str.length)
    // 判断是不是index结尾
    if (val === 'index') {
        return str.substring(index, -1)
    }
    return str
}

export default getRouter()
